package com.bdqn.tour.servlet.Gyh;

import com.bdqn.tour.service.GyhAddUserInfoService;
import com.bdqn.tour.service.impl.GyhAddUserInfoServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 管理后台添加业务
 */
@WebServlet("/GyhMsgAddUserInfo")
public class GyhMsgAddUserInfoServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //编写字符编码
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        //获取参数
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String phone = req.getParameter("phone");
        String status = req.getParameter("userType");
        //判断是否为管理员
        if(status.equals("admin")){
            status = "管理员";
        }else{
            status = "普通用户";
        }
        //调用service层
        GyhAddUserInfoService service = new GyhAddUserInfoServiceImpl();
        //调用service层在管理后端添加用户信息
        int row = service.addMsgUserInfo(phone,password,username,status);
        //根据row是否大于0判断是否添加成功
        if (row > 0){
            resp.getWriter().print("success");
        }else {
            resp.getWriter().print("error");
        }
        //关闭字符输出流
        resp.getWriter().close();


    }
}
